<?php
/**
 * Created by IntelliJ IDEA.
 * User: Jason
 * Date: 2018-12-03
 * Time: 15:12
 */

namespace app\member\controller;


use cmf\controller\AdminBaseController;
use think\Db;

/**
 * 商品控制器
 * Class AdminItemController
 * @package app\member\controller
 */
class AdminItemController extends AdminBaseController
{
    public function index()
{
    $item_name = request()->param('item_name');
    $where =['status'=>1000,'mark'=>2000];

    if($item_name){
        $this->assign('item_name',$item_name);
        $where['item_name']=['like','%'.$item_name.'%'];
    }

    $item = Db::name('member_item')->where($where)->paginate(20);
    $page= $item->render();

    $this->assign('item',$item);
    $this->assign('page',$page);
    return $this->fetch();
}

    public function add()
    {
        return $this->fetch();
    }

    public function addPost()
    {
        $now = time();
        $params = $this->request->param();
        $params['mark'] = 2000;
        $params['status'] = 1000;
        $params['add_time'] = $now;
        $params['update_time'] = $now;
        $res = Db::name('member_item')->insert($params);
        if($res)
            return $this->success("添加成功！",url('AdminItem/index'));
        else
            return $this->error('添加失败');
    }

    public function edit()
    {
        $id = $this->request->param('id');
        $item = Db::name('member_item')->where('id',$id)->find();
        $this->assign('item',$item);
        return $this->fetch();
    }

    public function editPost()
    {
        $now = time();
        $params = $this->request->param();
        $params['update_time'] = $now;
        $res = Db::name('member_item')->update($params);
        if($res)
            return $this->success("更新成功！",url('AdminItem/edit',array('id'=>$params['id'])));
        else
            return $this->error('更新失败');
    }

    public function delete()
    {
        $params = $this->request->param();
        $where['id'] = ['in',$params['ids'] ];
        $res = Db::name('member_item')->where($where)->update([ 'status'=>1001,'delete_time'=>time() ]);
        if($res)
            return $this->success("删除成功！");
        else
            return $this->error('删除失败');
    }

}